﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using TuLing.Entities;
using TuLing.Services;

namespace TuLing.WebApi.Server.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class FinancesController : ControllerBase
    {
        private readonly DefaultDbContext _context;

        public FinancesController(DefaultDbContext context)
        {
            _context = context;
        }

        // GET: api/Finances
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Finance>>> GetFinance()
        {
          if (_context.Finance == null)
          {
              return NotFound();
          }
            return await _context.Finance.ToListAsync();
        }

        // GET: api/Finances/5
        [HttpGet("{id}")]
        public async Task<ActionResult<Finance>> GetFinance(long id)
        {
          if (_context.Finance == null)
          {
              return NotFound();
          }
            var finance = await _context.Finance.FindAsync(id);

            if (finance == null)
            {
                return NotFound();
            }

            return finance;
        }

        // PUT: api/Finances/5
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPut("{id}")]
        public async Task<IActionResult> PutFinance(long id, Finance finance)
        {
            if (id != finance.Id)
            {
                return BadRequest();
            }

            _context.Entry(finance).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FinanceExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/Finances
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPost]
        public async Task<ActionResult<Finance>> PostFinance(Finance finance)
        {
          if (_context.Finance == null)
          {
              return Problem("Entity set 'DefaultDbContext.Finance'  is null.");
          }
            _context.Finance.Add(finance);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetFinance", new { id = finance.Id }, finance);
        }

        // DELETE: api/Finances/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteFinance(long id)
        {
            if (_context.Finance == null)
            {
                return NotFound();
            }
            var finance = await _context.Finance.FindAsync(id);
            if (finance == null)
            {
                return NotFound();
            }

            _context.Finance.Remove(finance);
            await _context.SaveChangesAsync();

            return NoContent();
        }

        private bool FinanceExists(long id)
        {
            return (_context.Finance?.Any(e => e.Id == id)).GetValueOrDefault();
        }
    }
}
